feat: guard Deployer singleton before Host construction#4189
feat: guard Deployer singleton before Host construction#4189m1rm wants to merge 1 commit intodeployphp:masterfrom
Conversation
|
Additional info/motivation of this PR: I came across the issue when implementing setDefaultSelector as thin typed wrapper for set('default_selector', ...) (which will be a subsequent PR). |
| $parent = null; | ||
| if (Deployer::get()) { | ||
| if (Deployer::hasInstance()) { | ||
| $parent = Deployer::get()->config; |
There was a problem hiding this comment.
What a use case where we create a Host instance before creating Deployer?
There was a problem hiding this comment.
Maybe we should throw an error here instead?
There was a problem hiding this comment.
What a use case where we create a Host instance before creating Deployer?
I ran into an error when trying to implement a thin typed wrapper for set('default_selector', ...), the wip implementation is here:
master...m1rm:deployer:feat/add-setDefaultSelector-function
maybe I also took a wrong turn, I'd be fine with throwing an error if that is more appropriate
Bug fix #…?Background
Host::__constructusedDeployer::get()to decide whether to inherit the global recipe config. With a typed, uninitialized static$instance, PHP 8+ throws when get() reads the property before new Deployer(...) has run. That can break HostTest (and any code path that builds a Host without a bootstrapped singleton).Solution
Deployer:
?self (default null).get()throws a clear RuntimeException if not initialized.hasInstance()so callers can check without touching an uninitialized property.resetInstance() (@internal) for tests that need a clean singleton between cases.Host:
Deployer::hasInstance()before callingget()when wiring the parent Configuration.HostTest:
tearDown()callsDeployer::resetInstance()so tests stay isolated.Deployer\Deployerfor tearDown.Behavior / compatibility
Deployer::get()with no instance: still invalid; error is now RuntimeException instead of the engine error on an uninitialized typed static.